import 'package:chat/controller/friend_detail_controller.dart';
import 'package:chat/model/friend_model.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

final FriendDetailController controller = Get.find();

class FriendDetail01Screen extends StatelessWidget {
  const FriendDetail01Screen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Obx(() {
        final person = controller.personDetail.value!;
        return CustomScrollView(
          slivers: [
            // 1. 吸顶的操作按钮行
            SliverAppBar(
              backgroundColor: Color(0XFFF2F3F7),
              elevation: 0,
              pinned: true,
              expandedHeight: 180,
              flexibleSpace: LayoutBuilder(
                builder: (context, constraints) {
                  final double progress =
                      (constraints.maxHeight - kToolbarHeight) / (180 - kToolbarHeight);
                  final double opacity = progress.clamp(0.0, 1.0);
                  return Stack(
                    children: [
                      Positioned(
                        top: 90 * opacity,
                        right: 40,
                        child: Opacity(
                          opacity: opacity * 0.9,
                          child: Transform.scale(
                            scale: 0.8 + 0.2 * opacity,
                            child: Image.asset(
                              'assets/images/background.png',
                              width: 150,
                              height: 150,
                              fit: BoxFit.contain,
                            ),
                          ),
                        ),
                      ),
                    ],
                  );
                },
              ),
              leading: IconButton(
                icon: Icon(Icons.arrow_back_ios, color: Colors.black),
                onPressed: () => Navigator.pop(context),
              ),
              actions: [
                IconButton(
                  icon: Icon(Icons.settings, color: Colors.black),
                  onPressed: () => Get.toNamed('/friendListScreen'),
                ),
              ],
            ),
            // 2. 主内容区域
            SliverList(
              delegate: SliverChildListDelegate([
                // 使用Stack实现吸底布局
                Stack(
                  children: [
                    // 背景层
                    Container(color: Color(0XFFF2F3F7), height: MediaQuery.of(context).size.height),
                    // 内容层
                    Positioned(
                      bottom: 0,
                      left: 0,
                      right: 0,
                      child: Column(
                        children: [
                          // 用户信息区域（上下圆角）
                          Container(
                            padding: EdgeInsets.all(10),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.vertical(
                                top: Radius.circular(20),
                                bottom: Radius.circular(20), // 添加底部圆角
                              ),
                            ),
                            child: Column(
                              children: [
                                _buildUserInfoSection(person),
                                SizedBox(height: 30),
                                _buildTab(person),
                              ],
                            ),
                          ),
                          // 操作选项区域
                          Container(
                            decoration: BoxDecoration(color: Colors.white),
                            child: Column(children: [_buildActionOptions(), _buildBottomButtons()]),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ]),
            ),
          ],
        );
      }),
    );
  }

  // 保持其他构建方法不变...
  Widget _buildUserInfoSection(Friend friendDetail) {
    return Row(
      children: [
        Stack(
          alignment: Alignment.bottomLeft,
          children: [
            CircleAvatar(radius: 50, backgroundImage: AssetImage(friendDetail.avatarUrl ?? "")),
            Positioned(
              right: 0,
              bottom: 0,
              child: Container(
                width: 28,
                height: 28,
                decoration: BoxDecoration(
                  color: Colors.white,
                  shape: BoxShape.circle,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.2),
                      blurRadius: 4,
                      offset: Offset(0, 2),
                    ),
                  ],
                ),
                child: Icon(Icons.edit, size: 16, color: Colors.black),
              ),
            ),
          ],
        ),
        const SizedBox(width: 16),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Text(
                    friendDetail.name,
                    style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
                  ),
                  Spacer(),
                  Icon(Icons.arrow_forward_ios, color: Colors.black, size: 16),
                ],
              ),
              SizedBox(height: 8),
              SizedBox(
                child: Row(
                  children: [
                    SizedBox(
                      width: 100,
                      child: Text(
                        friendDetail.ethAddress ?? "",
                        style: TextStyle(fontSize: 14, color: Colors.grey[600]),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    Icon(Icons.copy, color: Colors.grey[500], size: 16),
                  ],
                ),
              ),
              SizedBox(height: 8),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                    decoration: BoxDecoration(
                      color: Color(0xFFE1F166),
                      borderRadius: BorderRadius.circular(50),
                    ),
                    child: Text(
                      friendDetail.ppcInfo ?? "",
                      style: TextStyle(fontSize: 12, color: Colors.black),
                    ),
                  ),
                  SizedBox(width: 10),
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                    decoration: BoxDecoration(
                      color: Color(0xFFE1F166),
                      borderRadius: BorderRadius.circular(50),
                    ),
                    child: Text(
                      friendDetail.hertInfo ?? "",
                      style: TextStyle(fontSize: 12, color: Colors.black),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ],
    );
  }

  // 保持_tab、_wealth、_social等方法不变...
  Widget _buildTab(Friend friendDetail) {
    return SizedBox(
      height: 150,
      child: DefaultTabController(
        length: 2,
        child: Column(
          children: [
            TabBar(
              dividerHeight: 1,
              dividerColor: Color(0xFFF5F6F7),
              indicatorColor: Colors.black,
              labelColor: Colors.black,
              unselectedLabelColor: Colors.grey,
              labelStyle: TextStyle(fontWeight: FontWeight.bold),
              unselectedLabelStyle: TextStyle(fontWeight: FontWeight.bold),
              tabs: [Tab(text: "财富信息"), Tab(text: "社交信息")],
            ),
            Expanded(
              child: TabBarView(children: [_buildWealth(friendDetail), _buildSocial(friendDetail)]),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildWealth(Friend person) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(height: 10),
          Row(
            children: [
              Text('RFG', style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold)),
              Spacer(),
              Text('456666666', style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold)),
            ],
          ),
          SizedBox(height: 10),
          Row(
            children: [
              Text('空闲NFT', style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold)),
              Spacer(),
              Container(
                padding: EdgeInsets.all(2),
                decoration: BoxDecoration(
                  border: Border.all(color: Color(0xFFF5F6F7), width: 1.5),
                  borderRadius: BorderRadius.circular(50),
                ),
                child: Row(
                  children: [
                    Container(
                      width: 20,
                      height: 20,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        image: DecorationImage(
                          image: AssetImage('assets/images/zh-CN-XiaochenNeural.png'),
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                    SizedBox(width: 8),
                    Text('3a', style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold)),
                  ],
                ),
              ),
              SizedBox(width: 10),
              Container(
                padding: EdgeInsets.all(2),
                decoration: BoxDecoration(
                  border: Border.all(color: Color(0xFFF5F6F7), width: 1.5),
                  borderRadius: BorderRadius.circular(50),
                ),
                child: Row(
                  children: [
                    Container(
                      width: 20,
                      height: 20,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        image: DecorationImage(
                          image: AssetImage('assets/images/zh-CN-XiaochenNeural.png'),
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                    SizedBox(width: 8),
                    Text('2y', style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold)),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildSocial(Friend person) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16),
      child: Column(
        children: [
          SizedBox(height: 10),
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('Twitter', style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold)),
              Spacer(),
              Text('wechat', style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold)),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildActionOptions() {
    return Column(
      children: [
        _buildActionItem('设置备注和描述', controller.setRemark),
        _buildActionItem('删除联系人', controller.deleteContact),
        _buildActionItem('加入黑名单', controller.addToBlacklist),
      ],
    );
  }

  Widget _buildActionItem(String title, VoidCallback onTap) {
    return InkWell(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 12),
        child: Row(
          children: [
            Expanded(child: Text(title, style: TextStyle(fontSize: 16))),
            Icon(Icons.chevron_right, color: Colors.grey, size: 24),
          ],
        ),
      ),
    );
  }

  Widget _buildBottomButtons() {
    return Padding(
      padding: EdgeInsets.all(16.0),
      child: Column(
        children: [
          InkWell(
            onTap: controller.startChat,
            child: Container(
              width: double.infinity,
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(vertical: 10),
              decoration: BoxDecoration(
                color: Color(0XFFF2F3F7),
                borderRadius: BorderRadius.circular(20),
              ),
              child: Text(
                '发起聊天',
                style: TextStyle(color: Color(0xFF377E8D), fontWeight: FontWeight.bold),
              ),
            ),
          ),
          SizedBox(height: 16),
          InkWell(
            onTap: controller.startChat,
            child: Container(
              width: double.infinity,
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(vertical: 10),
              decoration: BoxDecoration(
                color: Color(0XFFF2F3F7),
                borderRadius: BorderRadius.circular(20),
              ),
              child: Text(
                '语音通话',
                style: TextStyle(color: Color(0xFF377E8D), fontWeight: FontWeight.bold),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
